package com.extsci.webmail.mapper;

import com.extsci.webmail.common.entity.sensitive.Sensitive;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author ccoke
 */
@Mapper
public interface SensitiveMapper {
    /**
     * 获取敏感词个数
     * @return 敏感词个数
     */
    @Select("<script>" +
            " SELECT count(*) FROM email_sensitive WHERE 1=1" +
            " <when test='keyword!=null'>" +
            " AND word LIKE concat('%',#{keyword},'%')" +
            " </when>" +
            "</script>")
    Integer selectSensitivesCount(@Param("keyword") String keyword);

    /**
     * 获取敏感词列表
     * @param start
     * @param pageSize
     * @return 敏感词列表
     */
    @Select("<script>" +
            " SELECT uuid, word, position, regular FROM email_sensitive WHERE 1=1" +
            " <when test='keyword!=null'>" +
            " AND word LIKE concat('%',#{keyword},'%')" +
            " </when>" +
            " ORDER BY create_at DESC" +
            " LIMIT #{start},#{pageSize}" +
            "</script>")
    List<Sensitive> selectSensitives(@Param("start") Integer start, @Param("pageSize") Integer pageSize, @Param("keyword") String keyword);

    /**
     * 根据位置获敏感词列表
     * @param a 位置a
     * @param b 位置b
     * @param c 位置c
     * @param d 位置d
     * @return 敏感词列表
     */
    @Select("SELECT word, regular FROM email_sensitive WHERE position = #{a} OR position = #{b} OR position = #{c} OR position = #{d} ORDER BY create_at DESC")
    List<Sensitive> selectSensitivesByPosition(@Param("a") Integer a, @Param("b") Integer b, @Param("c") Integer c, @Param("d") Integer d);

    /**
     * 添加敏感词
     * @param sensitive 敏感词信息
     * @return 影响行数 != 1 失败
     */
    @Insert("INSERT INTO email_sensitive VALUE(#{uuid}, #{word},#{position},#{regular}, #{createAt})")
    Integer insertSensitive(Sensitive sensitive);

    /**
     * 修改敏感词
     * @param sensitive 敏感词信息
     * @return 影响行数 != 1 失败
     */
    @Insert("UPDATE email_sensitive SET word = #{word}, position = #{position}, regular = #{regular} WHERE uuid = #{uuid}")
    Integer updateSensitive(Sensitive sensitive);

    /**
     * 删除指定敏感词
     * @param uuid 敏感词唯一ID
     * @return 影响行数 != 1 失败
     */
    @Delete("DELETE FROM email_sensitive WHERE uuid = #{uuid}")
    Integer deleteSensitiveByUuid(@Param("uuid") String uuid);

}
